package com.manage.platform.context;

import cn.hutool.core.util.ObjectUtil;
import com.manage.platform.exception.CommonException;
import com.manage.platform.exception.EmError;
import com.manage.platform.vo.UserVo;


/**
 * 用户信息缓存
 */
public class UserContext {


    private static final ThreadLocal<UserVo> userHolder = new ThreadLocal<>();

    public static void setUser(UserVo user) {
        userHolder.set(user);
    }

    public static UserVo getUser() {
        UserVo userVo = userHolder.get();
        if(ObjectUtil.isEmpty(userVo)){
            throw new CommonException(EmError.AUTH_TOKEN_EXPIRED);
        }
        return userVo;
    }

    public static void clearUser() {
        userHolder.remove();
    }

}
